<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Book;
use App\Services\TtsService;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class BookController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Book(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title')
                  ->width(200);
            $grid->column('cover')->image('',120,60);
            $grid->column('real_view_count');
            $grid->column('status')
                ->using([0 => '隐藏',1 => '显示'])
                ->label([0 => 'danger',1 => 'success']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->like('title','标题')->width(3);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Book(), function (Show $show) {
            $show->field('id');
            $show->field('sort');
            $show->field('title');
            $show->field('cover');
            $show->field('content');
            $show->field('view_count');
            $show->field('real_view_count');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Book(), function (Form $form) {
            $form->display('id');
            $form->number('sort')
                ->min(0)
                ->help('数字越小排序越靠前')
                ->required();
            $form->text('title')
                ->maxLength(255)
                ->required();
            $form->image('cover')
                 ->disk('oss')
                 ->autoUpload()
                 ->removable(false)
                 ->uniqueName()
                 ->saveFullUrl()
                 ->required();
            $form->editor('content')
                ->disk('oss')
                ->required();
            $form->number('view_count')
                ->min(0)
                ->help('数字越小排序越靠前')
                ->required();

            $form->radio('status')
                ->options([1 => '显示',0 => '隐藏'])
                ->default(1)
                ->required();

            $form->hidden('task_id');
            $form->hidden('audio_url');

            $form->saving(function (Form $form){
                 if ($form->isCreating()){
                     $tts = new TtsService();
                     $task_id = $tts->ttsCreate(str_replace('&nbsp;','',strip_tags($form->input('content'))));
                     $form->input('task_id',$task_id);

                 }

                 if ($form->isEditing() && $form->input('content') != $form->model()->content){
                     $tts = new TtsService();
                     $task_id = $tts->ttsCreate(strip_tags($form->input('content')));
                     $form->input('task_id',$task_id);
                     $form->input('audio_url','');
                 }
                 $form->input('task_id',(string)$form->input('task_id'));
                 $form->input('audio_url',(string)$form->input('audio_url'));
            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
